<?php

include('../../include/functions.inc.php');
if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 'refresh':
            loadCareer($db);
            break;
        case 'new':
            save($db, $user);
            break;
        case 'edit':
            update($db, $user);
            break;
        case 'del':
            delete($db, $user);
            break;
        case 'prev':
            prevAssign($db, $user);
            break;
        case 'stud':
            listStudents();
            break;
    }
}

function prevAssign($db, $user) {
    $prev = findTeacherAssigns($db, getParam("ID"), $user, getParam("CODE"), getParam("SECTION"));
    $data = array('ST' => 1);
    if ($prev) {
        $data = array('ST' => 0, 'ERROR' => 'El Catedratico ya tiene asignado este curso!');
    }
    $_SESSION['pack'] = serialize($data);
    header('Location: ../../include/transport.php');
}

function save($db, $user) {
    $teacher = getParam('ID');
    $cy = getCycle($db, $user);

    //asigna estudiante
    $db->query_insert('cllg_teacher_assign', array(
        'FK_TEACHER' => sqlValue($teacher, 'text'),
        'FK_COURSE' => sqlValue(getParam("CODE"), 'int'),
        'FK_CYCLE' => sqlValue(getParam("CYCLE"), 'int'),
        'FK_CAREER' => sqlValue(getParam("FK_CAREER"), 'int'),
        'STATUS' => 1,
        'SECTION' => sqlValue(getParam('SECTION'), 'text'),
        'LUNES' => sqlValue(getParam('LUNES'), 'int'),
        'MARTES' => sqlValue(getParam('MARTES'), 'int'),
        'MIERCOLES' => sqlValue(getParam('MIERCOLES'), 'int'),
        'JUEVES' => sqlValue(getParam('JUEVES'), 'int'),
        'VIERNES' => sqlValue(getParam('VIERNES'), 'int'),
        'SABADO' => sqlValue(getParam('SABADO'), 'int'),
        'TIME_BEGIN' => sqlValue(getParam('H_INICIO'), 'text'),
        'TIME_END' => sqlValue(getParam('H_FIN'), 'text'),
        'DATE_START' => sqlValue(getParam('F_INICIO'), 'date'),
        'DATE_END' => sqlValue(getParam('F_FINAL'), 'date'),
        'CREATED_BY' => sqlValue($user->name, 'text'),
        'CREATED' => sqlValue(date("y-m-d"), 'date')));
    header('Location: index.php?ID=' . $teacher . "&NAME=" . getParam("NAME"));
}

function update($db, $user) {
    $teacher = getParam('ID');
    $cy = getCycle($db, $user);

    //asigna estudiante
    $db->query_update('cllg_teacher_assign', array(
        'FK_COURSE' => sqlValue(getParam("CODE"), 'int'),
        'FK_CAREER' => sqlValue(getParam("FK_CAREER"), 'int'),
        'SECTION' => sqlValue(getParam('SECTION'), 'text'),
        'LUNES' => sqlValue(getParam('LUNES'), 'int'),
        'MARTES' => sqlValue(getParam('MARTES'), 'int'),
        'MIERCOLES' => sqlValue(getParam('MIERCOLES'), 'int'),
        'JUEVES' => sqlValue(getParam('JUEVES'), 'int'),
        'VIERNES' => sqlValue(getParam('VIERNES'), 'int'),
        'SABADO' => sqlValue(getParam('SABADO'), 'int'),
        'TIME_BEGIN' => sqlValue(getParam('H_INICIO'), 'text'),
        'TIME_END' => sqlValue(getParam('H_FIN'), 'text'),
        'DATE_START' => sqlValue(getParam('F_INICIO'), 'date'),
        'DATE_END' => sqlValue(getParam('F_FINAL'), 'date')), "ID=" . sqlValue(getParam("IDASSIGN"), "int"));
    header('Location: index.php?ID=' . $teacher . "&NAME=" . getParam("NAME"));
}

function delete($db, $user) {
    $teacher = getParam('ID');
    $db->query_delete("cllg_teacher_assign", "ID=" . sqlValue(getParam("IDASSIGN"), "int"));
    header('Location: index.php?ID=' . $teacher . "&NAME=" . getParam("NAME"));
}

?>